package com.masterclient.app.modules.purchase;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselibrary.bean.aftersale.ShipmentsBean;
import com.eoner.baselibrary.bean.purchase.DeliveryDetailBean;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.modules.purchase.contract.PurchaseShippingContract;
import com.masterclient.app.modules.purchase.contract.PurchaseShippingPresenter;
import com.masterclient.app.widget.TitlebarView;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @auther hjian
 * @date 2020/9/22.
 */
@Route(path = AroutePath.PURCHASE_SHIPPING_ACTIVITY)
public class PurchaseShippingActivity extends BaseActivity<PurchaseShippingPresenter> implements PurchaseShippingContract.View {

    @BindView(R.id.purchase_shipping_bar)
    TitlebarView purchaseShippingBar;
    @BindView(R.id.purchase_shipping_name)
    TextView purchaseShippingName;
    @BindView(R.id.purchase_shipping_mobile)
    TextView purchaseShippingMobile;
    @BindView(R.id.purchase_shipping_address)
    TextView purchaseShippingAddress;
    @BindView(R.id.purchase_shipping_courier_number)
    EditText purchaseShippingCourierNumber;
    @BindView(R.id.purchase_shipping_courier_company)
    TextView purchaseShippingCourierCompany;
    @BindView(R.id.purchase_shipping_courier_company_layout)
    ConstraintLayout purchaseShippingCourierCompanyLayout;
    @BindView(R.id.purchase_shipping_confirm)
    TextView purchaseShippingConfirm;
    @BindView(R.id.purchase_shipping_root)
    LinearLayout purchaseShippingRoot;

    @Autowired(name = "order_id")
    String orderId;
    @Autowired(name = "type")
    int type;

    private List<ShipmentsBean.DataBean.ShShipmentCompanysBean> list;
    private String code;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_purchase_shipping);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        mPresenter.getShippingData(orderId,"");
        mPresenter.getShippingList();
        purchaseShippingBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
    }

    @OnClick({R.id.purchase_shipping_courier_company_layout, R.id.purchase_shipping_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.purchase_shipping_courier_company_layout:
                if (list == null){
                    return;
                }
                ARouter.getInstance().build(AroutePath.LOGISTICS_COMPANY_ACTIVITY)
                        .withSerializable("list",(Serializable)list)
                        .navigation(this,0x127);
                break;
            case R.id.purchase_shipping_confirm:
                String orderNo = purchaseShippingCourierNumber.getText().toString();
                if (TextUtils.isEmpty(orderNo)) {
                    ToastUtil.showToast("请输入快递单号");
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    ToastUtil.showToast("请选择快递公司");
                    return;
                }
                purchaseShippingConfirm.setEnabled(false);
                mPresenter.confirmShipping(type,orderId,orderNo,code);

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x127&&resultCode == RESULT_OK){
            ShipmentsBean.DataBean.ShShipmentCompanysBean bean = (ShipmentsBean.DataBean.ShShipmentCompanysBean) data.getSerializableExtra("bean");
            purchaseShippingCourierCompany.setText(bean.getSh_name());
            purchaseShippingCourierCompany.setTextColor(Color.parseColor("#141922"));
            code = bean.getSh_code();
        }
    }

    @Override
    public void showShippingList(List<ShipmentsBean.DataBean.ShShipmentCompanysBean> list) {
        this.list = list;
    }

    @Override
    public void showResult(boolean isSuccess, String msg) {
        purchaseShippingConfirm.setEnabled(true);
        ToastUtil.showToast(msg);
        if (isSuccess){
            finish();
        }
    }

    @Override
    public void showData(DeliveryDetailBean data) {
        purchaseShippingName.setText(data.getReceive_info().getName());
        purchaseShippingMobile.setText(data.getReceive_info().getMobile());
        purchaseShippingAddress.setText(data.getReceive_info().getAddress());

        if (data.getExpress_info()!= null){
            code = data.getExpress_info().getCarrier_code();
            purchaseShippingCourierCompany.setText(TextUtils.isEmpty(data.getExpress_info()
                    .getShipment_name())?"请选择快递公司":data.getExpress_info().getShipment_name());
            purchaseShippingCourierCompany.setTextColor(Color.parseColor(TextUtils.isEmpty(data.getExpress_info()
                    .getShipment_name())?"#999faa":"#141922"));
            purchaseShippingCourierNumber.setText(TextUtils.isEmpty(data.getExpress_info()
                    .getTrack_no())?"":data.getExpress_info().getTrack_no());
        }

        purchaseShippingRoot.setVisibility(View.VISIBLE);
    }

}
